/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elookinto.blackhat;

import java.io.FileWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class SeliumBaike {

    static WebElement connections;
    static WebDriver driver;
    static boolean readOnly = false;

    public static void main(String[] args) throws Exception {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        //File file = new File("C:/projs/iedriverserver.exe");
        //System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
       System.setProperty("webdriver.chrome.driver", "C:\\tools\\chromedriver.exe");
        driver = new ChromeDriver();
        //driver = new HtmlUnitDriver();
        //driver = new FirefoxDriver();
        // driver.get("http://baike.baidu.com/view/1.htm");
        for (int i = 4829; i < 10000; i++) {
            String baseUrl = "http://baike.baidu.com/view/" + i + ".htm";
            //WebDriverBackedSelenium selenium = new WebDriverBackedSelenium(driver, baseUrl);

            //Thread.currentThread().sleep(2000);
            //Actions action= new Actions(driver); 
            // action.moveByOffset(10, 10);
            //DefaultSelenium.contextMenuAt();
            //action.contextClick().build().perform();
            //WebElement we = driver.findElement(By.id("content"));
            //System.out.println(we.getText());
            //String p = we.getText();
            try {
                driver.get("http://translate.google.com/#auto/en/");
                Thread.currentThread().sleep(2000);
                driver.findElement(By.id("source")).sendKeys(baseUrl);
                System.out.println("here");
                driver.findElement(By.id("gt-submit")).click();
                Thread.currentThread().sleep(3000);
                driver.switchTo().frame("c");
                System.out.println("here2");
                WebElement we = driver.findElement(By.id("content"));
                //System.out.println(we.getText());
                //System.out.println(we.getAttribute("innerHTML"));
               String q = "";
               try {
               q=driver.findElement(By.id(""+i)).getText();
               } catch(Exception e2){
                   e2.printStackTrace();
                    q=driver.findElement(By.className("title")).getText();
               }
               
                FileWriter fw = new FileWriter("c:/baike/1747/t" + i + ".html");
                
                System.out.println(q);
               fw.append("<html> <head><meta http-equiv=\"refresh\" content=\"1;url=http://www.elookinto.com/search?q=" + java.net.URLEncoder.encode(q)+ "\" >");
               fw.append("</head><body><pre>");
                fw.append(we.getText());
                fw.append("</pre></body></html>");
                fw.close();

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        driver.quit();


    }

    static void sending(String email) throws Exception {
        Connection con = null;
        try {
            con = Util.getCon();
            PreparedStatement st = con.prepareStatement("SELECT k.url, k.id from linkedin as k left join sent s  on k.id = s.refid  where s.invite_time is  null and (  email is null)");
            //st.setString(1, email);
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                String url = rs.getString(1);
                int refid = rs.getInt(2);
                try {
                    forUrl(url, email, refid);
                    if (!readOnly) {
                        Thread.currentThread().sleep(15000);
                    }
                } catch (Exception urle) {
                    urle.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            con.close();
        }
    }

    static void forUrl(String url, String email, int refid) throws Exception {
        driver.get(url);
        Connection con = null;
        try {
            con = Util.getCon();
            PreparedStatement ps = con.prepareStatement("INSERT INTO sent (id, email, refid, invite_time) values(null, ?, ?, current_timestamp) ");
            ps.setString(1, email);
            ps.setInt(2, refid);
            if (!readOnly) {
                ps.execute();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            con.close();
        }
        //Robot robot = new Robot();
        Actions act = new Actions(driver);
        try {
            WebElement mainMenu = driver.findElement(By.className("connect-menu-toggle"));
            act.moveToElement(mainMenu).build().perform();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            WebElement connMenu = driver.findElement(By.linkText("Connect"));
            //if (true) return;
            if (!readOnly) {
                connMenu.click();
            }

        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        driver.findElement(By.id("IF-reason-iweReconnect")).click();
        driver.findElement(By.id("greeting-iweReconnect")).clear();
        driver.findElement(By.id("greeting-iweReconnect")).sendKeys("I'd like to add you to my professional network on LinkedIn.\n\n- Steve from Elookinto");
        driver.findElement(By.name("iweReconnectSubmit")).click();

    }
}